package com.itboye.textileexpres.myset;

import java.util.Observable;
import java.util.Observer;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.itboye.textileexpes.beans.ReceiveAddressBean;
import com.itboye.textileexpes.presenter.ReceiveAddressPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.SPContants;
import com.itboye.textileexpres.util.SPUtils;

public class ActivityReceiptAddress extends BaseActivity implements
		 Observer ,TextWatcher{


	protected String cityTxt;
	private TextView edregion;
	private Button btn_send;

	private EditText edName;

	private EditText edPhoneName;

	private EditText edaddressName;

	private EditText edPostcodeName;

	ReceiveAddressBean model;
	ImageView back;
	TextView title;
	ImageView img_other;
	TextView txt_save;
	private String id;
	private String isDefault;
	ToggleButton default_address;
	ReceiveAddressPresenter addressPresenter;
	String addCity;
	
	private String countryID;
	private String provinceID;
	private String cityID;
	private String areaID;
	
	private String countryNmae;
	private String provinceNmae;
	private String cityNmae;
	private String areaNmae;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_addree_huowu;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub TitleCity
		title.setText("新增地址");
		IsMobileNumPhone.isSPUtils(getApplication());
		img_other.setVisibility(View.GONE);
		txt_save.setVisibility(View.VISIBLE);
		
		edPostcodeName.addTextChangedListener(this);
		txt_save.setEnabled(false);
		addressPresenter = new ReceiveAddressPresenter(this);
		IntentFilter intentFilterAvart2 = new IntentFilter("province");
		this.registerReceiver(avartBroadcastReceiver, intentFilterAvart2);
		
		
		// 其他国家
		IntentFilter intentFilterAvart3 = new IntentFilter("AddressBeans");
		this.registerReceiver(avartBroadcastReceiverTitle, intentFilterAvart3);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		txt_save.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onEnaBled();
			}
		});
		edregion.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				Intent intent = new Intent(ActivityReceiptAddress.this,
						ActivityDiZhiType.class);
				intent.putExtra("type", "3");
				startActivity(intent);
			}
		});
	}

	

	BroadcastReceiver avartBroadcastReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {

			addCity = (String) SPUtils.get(ActivityReceiptAddress.this, null,
					SpPublicParams.TilteCity, "");
			edregion.setText(addCity);
		};
	};
	BroadcastReceiver avartBroadcastReceiverTitle = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			addCity = (String) SPUtils.get(ActivityReceiptAddress.this, null,
					SpPublicParams.AddressBeans, "");
			
			edregion.setText(addCity);
			
			countryID=(String) SPUtils.get(ActivityReceiptAddress.this, null,
					SpPublicParams.COUTRYID, "");
			provinceID=(String) SPUtils.get(ActivityReceiptAddress.this, null,
					SpPublicParams.PROVINCEID, "");
			
			cityID=(String) SPUtils.get(ActivityReceiptAddress.this, null,
					SpPublicParams.PROVINCECITYID, "");
			areaID=(String) SPUtils.get(ActivityReceiptAddress.this, null,
					SpPublicParams.AREAID, "");
			
			
			countryNmae=(String) SPUtils.get(ActivityReceiptAddress.this, null,
					SpPublicParams.COUNTRY, "");
			provinceNmae=(String) SPUtils.get(ActivityReceiptAddress.this, null,
					SpPublicParams.PROVINCE, "");
			
			cityNmae=(String) SPUtils.get(ActivityReceiptAddress.this, null,
					SpPublicParams.PROVINCECITY, "");
			areaNmae=(String) SPUtils.get(ActivityReceiptAddress.this, null,
					SpPublicParams.AREA, "");
			
			
			
			System.out.print("countryid"+areaNmae);
		};
	};

	public void onEnaBled() {
//		showProgressDialog("正在注册中，请稍后", true);
		model = new ReceiveAddressBean();
		if (default_address.isChecked()) {
			isDefault = "0";

		} else {
			isDefault = "1";
		}
		String people = edName.getText().toString().trim();
		String phone = edPhoneName.getText().toString().trim();
		String addrDetail = edaddressName.getText().toString();
		String code = edPostcodeName.getText().toString().trim();
		String address = edregion.getText().toString().trim();
		if ("".equals(people) || people == "") {
			toast("请填写姓名");
			return;
		}
		if (IsMobileNumPhone.isMobileNum(phone)==false) {
			toast("手机格式不对！");
			return;
		}else if(phone==null || phone==""){
			toast("手机不能为空！");
			return ;
		}
		if ("".equals(addrDetail) || addrDetail == "") {

			toast("请填写详细地址");
			return;
		}
		if (IsMobileNumPhone.isZipNO(code)==false) {
			toast("邮编格式不对");
			return;
		}
		if (address==null || address=="") {
			toast("请选择地址");
			return;
		}
		String countryid = (String) SPUtils.get(ActivityReceiptAddress.this,
				null, SpPublicParams.CountryID, "");
		if ("".equals(countryid) || countryid == "" || countryid == null) {
			toast("请先选择区域");
			try {
				closeProgressDialog();
			} catch (Exception e) {
				// TODO: handle exception
			}
			Intent intent = new Intent(ActivityReceiptAddress.this,
					ActivityDiZhiType.class);
			startActivity(intent);
			return;
		} else {
			model.setcountryId("1");
		}
		Log.e("hu--", countryid);
		model.setUid(IsMobileNumPhone.isUid());
		model.setCountry(countryNmae);
		model.setProvinceid(provinceID);
		model.setCityid(cityID);
		model.setAreaid(areaID);
		model.setDetailinfo(addrDetail);
		model.setContactname(people);
		model.setMobile(phone);
		model.setpostalCode(code);
		model.setArea(areaNmae);
		model.setProvince(provinceNmae);
		model.setCity(cityNmae);
		model.setisDefault(isDefault);
		addressPresenter.add(model);

		// String ednameEdittxt = edName.getText().toString();
		// String ePhone = edPhoneName.getText().toString();
		// String edaddree = edaddressName.getText().toString();
		// String edpostcode = edPostcodeName.getText().toString();
		// if (ednameEdittxt.equals("") || ePhone.equals("")
		// || edaddree.equals("") || edpostcode.equals("")) {
		// btn_send.setEnabled(false);
		// } else {
		// btn_send.setEnabled(true);
		// }
	}

	

	
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == ReceiveAddressPresenter.ADDSuccessEvent) {
				toast(entity.getData() + "");
				finish();
//				try {
//					closeProgressDialog();
//				} catch (Exception e) {
//					// TODO: handle exception
//				}
			}
		} else {

		}

	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
		if(edPostcodeName.getText().toString().length()>0){
			txt_save.setEnabled(true);
		}else{
			txt_save.setEnabled(false);
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
